package jdk8.lambda;

import jdk8.lambda.imterface.Converter;
import jdk8.lambda.imterface.MyFunction;

/**
 * @Desc:   Lambda 几种场景
 * @author: cww
 * @DateTime: 2020-06-06 16:14
 */

public class LambdaTest {
    public static void main(String[] args) {
        testImpl1();
    }

    public static void testRunnable() {
        Runnable run = () -> System.out.println("hello lambda");
        run.run();

    }

    /**
     * lambda 配合函数式接口的使用案例
     * lambda表达式必须和函数式接口的抽象函数描述一样的参数类型  x -> x*x  / x -> x+x
     * 它的返回类型也必须和抽象函数的返回类型兼容，并且他能抛出的异常也仅限于在函数的描述范围中
     */
    public static void testImpl() {
        Integer res = operation(200, x -> x*x);
        Integer res1 = operation(200, x -> x+x);
        System.out.println(res);
        System.out.println(res1);
    }

    public static Integer operation(Integer num, MyFunction myFunction){
        return myFunction.getValue(num);
    }

    /**
     * 带泛型的 函数 lam
     */
    public static void testImpl1() {
        Converter<String, Integer> converter = s -> Integer.valueOf(s);
        Integer i = converter.convert("12");
        System.out.println(i);
    }

    /**
     * 线程使用 lam 例子
     */
    public static void threadLam() {
        Thread t = new Thread(() -> {
            System.out.println("runnable");
        });
        t.start();
    }

}
